package com.vibration.web.controller.baseinfo;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.vibration.common.core.domain.entity.TreeNode;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.vibration.common.annotation.Log;
import com.vibration.common.core.controller.BaseController;
import com.vibration.common.core.domain.AjaxResult;
import com.vibration.common.enums.BusinessType;
import com.vibration.baseinfo.domain.CzEquipment;
import com.vibration.baseinfo.service.ICzEquipmentService;
import com.vibration.common.utils.poi.ExcelUtil;
import com.vibration.common.core.page.TableDataInfo;

/**
 * 设备管理Controller
 * 
 * @author wu
 * @date 2022-08-11
 */
@RestController
@RequestMapping("/baseinfo/equipment")
public class CzEquipmentController extends BaseController
{
    @Autowired
    private ICzEquipmentService czEquipmentService;

    /**
     * 查询设备管理列表
     */
    @PreAuthorize("@ss.hasPermi('baseinfo:equipment:list')")
    @GetMapping("/list")
    public TableDataInfo list(CzEquipment czEquipment)
    {
        startPage();
        List<CzEquipment> list = czEquipmentService.selectCzEquipmentList(czEquipment);
        return getDataTable(list);
    }

    /**
     * 查询设备管理列表
     */
    @PreAuthorize("@ss.hasPermi('baseinfo:equipment:list')")
    @GetMapping("/listEquipment")
    public AjaxResult listEquipment(CzEquipment czEquipment)
    {
        List<CzEquipment> list = czEquipmentService.selectCzEquipmentList(czEquipment);

        return AjaxResult.success(list);
    }

    /**
     * 查询设备管理列表
     */
    @PreAuthorize("@ss.hasPermi('baseinfo:equipment:list')")
    @GetMapping("/equipmentList")
    public AjaxResult equipmentList(CzEquipment czEquipment)
    {
        List<CzEquipment> list = czEquipmentService.selectCzEquipmentList(czEquipment);
        List<TreeNode> treeList = new ArrayList<>();
        for (CzEquipment equipment : list) {
            TreeNode node = new TreeNode();
            node.setId(equipment.getSn());
            node.setLabel(equipment.getEquipmentName());
            node.setParentId(0L);
            treeList.add(node);
        }
        return AjaxResult.success(treeList);
    }

    /**
     * 导出设备管理列表
     */
    @PreAuthorize("@ss.hasPermi('baseinfo:equipment:export')")
    @Log(title = "设备管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CzEquipment czEquipment)
    {
        List<CzEquipment> list = czEquipmentService.selectCzEquipmentList(czEquipment);
        ExcelUtil<CzEquipment> util = new ExcelUtil<CzEquipment>(CzEquipment.class);
        util.exportExcel(response, list, "设备管理数据");
    }

    /**
     * 获取设备管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('baseinfo:equipment:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(czEquipmentService.selectCzEquipmentById(id));
    }

    /**
     * 新增设备管理
     */
    @PreAuthorize("@ss.hasPermi('baseinfo:equipment:add')")
    @Log(title = "设备管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CzEquipment czEquipment)
    {
        //默认设备默认坐标 南京地质调查中心
        czEquipment.setLat(32.089635);
        czEquipment.setLon(118.928286);
        return toAjax(czEquipmentService.insertCzEquipment(czEquipment));
    }

    /**
     * 修改设备管理
     */
    @PreAuthorize("@ss.hasPermi('baseinfo:equipment:edit')")
    @Log(title = "设备管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CzEquipment czEquipment)
    {
        return toAjax(czEquipmentService.updateCzEquipment(czEquipment));
    }

    /**
     * 删除设备管理
     */
    @PreAuthorize("@ss.hasPermi('baseinfo:equipment:remove')")
    @Log(title = "设备管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(czEquipmentService.deleteCzEquipmentByIds(ids));
    }
}
